package cn.pengyinrui.pethome.emp.service.impl;

import cn.pengyinrui.pethome.base.query.BaseQuery;
import cn.pengyinrui.pethome.base.service.impl.BaseServiceImpl;
import cn.pengyinrui.pethome.emp.domain.Employee;
import cn.pengyinrui.pethome.emp.domain.EmployeeBo;
import cn.pengyinrui.pethome.emp.mapper.EmployeeMapper;
import cn.pengyinrui.pethome.emp.query.EmployeeQuery;
import cn.pengyinrui.pethome.emp.service.IEmployeeService;
import cn.pengyinrui.pethome.utils.MyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    // findAllBoByQuery
    @Autowired
    private EmployeeMapper mapper;

    private List<Employee> copyListBackNew(List<EmployeeBo> sources){
        List<Employee> employees = new ArrayList<Employee>();

        for (EmployeeBo source : sources) {
            Employee employee = new Employee();
            BeanUtils.copyProperties(source,employee);
            employees.add(employee);
        }

        return employees;
    }

    @Override
    public List<Employee> findAll() {
        List<EmployeeBo> list = mapper.findAllBo();
        return copyListBackNew(list);
    }

    @Override
    public List<Employee> findAllByQuery(BaseQuery query) {
        List<EmployeeBo> allBoByQuery = mapper.findAllBoByQuery();
        return copyListBackNew(allBoByQuery);
    }

    @Override
    @Transactional
    public void add(Employee employee) {
        mapper.add(employee);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        mapper.update(employee);
    }

}
